package service

import (
	"context"
	"fmt"
	"fresh-food/request"
	"fresh-food/response"
	"github.com/gin-gonic/gin"
	"github.com/jjl309/oneGroup"
	"math/rand"
	"time"
)

func SendSms(c *gin.Context) {
	var data request.SendSmsReq
	if err := c.ShouldBind(&data); err != nil {
		response.CurrencyErrResponse(c, err.Error(), 101)
		return
	}
	code := rand.Intn(9000) + 1000
	key := fmt.Sprintf("code:%s", data.Tel)
	/*sms, err := oneGroup.Sms(data.Tel, strconv.Itoa(code))
	if err != nil {
		response.CurrencyErrResponse(c, err.Error(), -1)
		return
	}
	if *sms.Body.Code != "OK" {
		response.CurrencyErrResponse(c, *sms.Body.Message, -1)
		return
	}*/
	err := oneGroup.Rdb.Set(context.Background(), key, code, 5*time.Minute).Err()
	if err != nil {
		response.CurrencyErrResponse(c, err.Error(), -1)
		return
	}
	response.CurrencySuccessResponse(c, "短信验证码发送成功", nil)
}
